<template>
    <view class="page">
        <view class="page-header">
            <view class="page-header-top">
                <view class="header-top-left">
                    <img class="img" v-if="type==0" :src="File_Url+'default.png'" />
                    <img class="img" v-if="type==1" :src="userInfo.user_info.photo_url" />
                    <img class="icon" :src="File_Url+'vip-icon.png'" v-if="userInfo.user_info.is_vip==1" />
                </view>
                <view class="header-top-center" v-if="type==1">
                    <view class="user-name" >
                        {{userInfo.user_info.nickname}}
<!--                        <img class="icon" :src="File_Url+'vip-icon.png'" v-if="userInfo.user_info.is_vip==1" />-->
                    </view>
                    <view class="user-phone" v-if="userInfo.phone!=null">{{userInfo.phone}}</view>
                    <view class="user_unit_name" v-if="userInfo.user_info.company_name!=null">{{userInfo.user_info.company_name}}</view>
                </view>
                <view class="header-top-center" v-if="type==0">
                    <button open-type="getUserInfo" @getuserinfo="bindGetUserInfo" class="notloggedin">点击登录</button>
                </view>
                <view class="header-top-right" @click="skipEditPerson" v-if="type==1">
                    <text>个人资料</text>
                    <img :src="File_Url+'right-white.png'" />
                </view>
            </view>
            <view class="page-header-bottom">
                <view class="header-bottom-item" @click="skipMyFollow(0)">
                    <text class="item-number">{{userInfo.user_info.user_attention_News_num?userInfo.user_info.user_attention_News_num:'0'}}</text>
                    <text class="item-title">关注资讯</text>
                </view>
                <view class="header-bottom-line"></view>
                <view class="header-bottom-item" @click="skipMyFollow(1)">
                    <text class="item-number">{{userInfo.user_info.user_attention_demand_num?userInfo.user_info.user_attention_demand_num:'0'}}</text>
                    <text class="item-title">关注服务</text>
                </view>
                <view class="header-bottom-line"></view>
                <view class="header-bottom-item" @click="skipMyFollow(2)">
                    <text class="item-number">{{userInfo.user_info.user_attention_activity_num?userInfo.user_info.user_attention_activity_num:'0'}}</text>
                    <text class="item-title">关注活动</text>
                </view>
            </view>
        </view>
        <view class="page-body-list">
            <view class="body-list-item" @click="skipReleaseNeeds">
                <view class="list-item-img">
                    <img :src="File_Url+'myneeds.png'" />
                </view>
                <view class="list-item-title">发布的服务</view>
                <view class="list-item-right">
                    <text>{{userInfo.user_info.user_release_demand_num}}</text>
                    <img :src="File_Url+'right-grey.png'" />
                </view>
            </view>
            <view class="body-list-item" @click="skipButtjointNeeds">
                <view class="list-item-img">
                    <img :src="File_Url+'dockingneeds.png'" />
                </view>
                <view class="list-item-title">对接的服务</view>
                <view class="list-item-right">
                    <text>{{userInfo.user_info.user_butt_demand_num}}</text>
                    <img :src="File_Url+'right-grey.png'" />
                </view>
            </view>
            <view class="body-list-item" @click="skipMyActivity">
                <view class="list-item-img">
                    <img :src="File_Url+'myactivity.png'" />
                </view>
                <view class="list-item-title">我的活动</view>
                <view class="list-item-right">
                    <text>{{userInfo.user_info.user_join_activity_num}}</text>
                    <img :src="File_Url+'right-grey.png'" />
                </view>
            </view>
        </view>
        <view class="page-body-list">
            <view class="body-list-item" @click="skipAuthentication">
                <view class="list-item-img">
                    <img :src="File_Url+'authentication.png'" />
                </view>
                <view class="list-item-title">认证身份</view>
                <view class="list-item-right">
                    <text>去认证</text>
                    <img :src="File_Url+'right-grey.png'" />
                </view>
            </view>
<!--            <view class="body-list-item"  @click="skipLogin" v-if="userInfo.phone==null">-->
<!--                <view class="list-item-img">-->
<!--                    <img :src="File_Url+'bindmobile.png'" />-->
<!--                </view>-->
<!--                <view class="list-item-title">绑定手机号</view>-->
<!--                <view class="list-item-right">-->
<!--                    <text>未绑定</text>-->
<!--                    <img :src="File_Url+'right-grey.png'" />-->
<!--                </view>-->
<!--            </view>-->
<!--            <view class="body-list-item" v-if="userInfo.phone!=null">-->
<!--                <view class="list-item-img">-->
<!--                    <img :src="File_Url+'bindmobile.png'" />-->
<!--                </view>-->
<!--                <view class="list-item-title">绑定手机号</view>-->
<!--                <view class="list-item-right">-->
<!--                    <text>已绑定</text>-->
<!--                    <img :src="File_Url+'right-grey.png'" />-->
<!--                </view>-->
<!--            </view>-->
        </view>
        <view class="page-body-list">
            <button open-type="contact" class="body-list-item">
                <view class="list-item-img">
                    <img :src="File_Url+'onlineservice.png'" />
                </view>
                <view class="list-item-title">在线客服</view>
                <view class="list-item-right">
                    <img :src="File_Url+'right-grey.png'" />
                </view>
            </button>
            <view class="body-list-item" @click="skipAboutUs">
                <view class="list-item-img">
                    <img :src="File_Url+'aboutus.png'" />
                </view>
                <view class="list-item-title">关于我们</view>
                <view class="list-item-right">
                    <img :src="File_Url+'right-grey.png'" />
                </view>
            </view>
        </view>

    </view>
</template>

<script>
    export default {
        name: "person",
        data(){
            return{
                File_Url: this.$api.FILE_URL,
                userInfo: {},
                type: 0,
                is_subscribe: true
            }
        },
        onLoad(){
            // if (uni.getStorageSync("userToken")){
            //     this.getUserInfos();
            // }else {
            //     this.type = 0
            // }
        },
        onShow(){
            this.getUserInfos();
            // if (uni.getStorageSync("userToken")){
            //     this.getUserInfos();
            // }else {
            //     this.type = 0
            // }
        },
        methods:{
            //订阅消息判断
            Subscribe(item){
                let _this = this;
                wx.getSetting({
                    withSubscriptions: true,
                    success (res) {
                        console.log(res)
                        if (res.subscriptionsSetting.mainSwitch==false) {
                            uni.showToast({
                                icon: 'none',
                                title: '请点击右上角进入设置开启接收订阅消息~',
                                duration: 3000
                            })
                        }else {
                            console.log(uni.getStorageSync('timeSubscribe'))
                            if (uni.getStorageSync('timeSubscribe')){
                                let nowtime = new Date();
                                let year = nowtime.getFullYear();
                                let month = nowtime.getMonth()+1;
                                let day = nowtime.getDate();
                                nowtime = year+'-'+month+'-'+day;
                                let dateSpan = Date.parse(nowtime) - Date.parse(uni.getStorageSync('timeSubscribe'));
                                dateSpan = Math.abs(dateSpan);
                                let iDays = Math.ceil(dateSpan / (24 * 3600 * 1000));
                                if (iDays>6){
                                    _this.showModel(item)
                                }
                            }else{
                                _this.showModel(item)
                            }
                        }
                    }
                })
            },
            //弹窗提示
            showModel(item){
                let _this = this;
                uni.showModal({
                    title: '提示',
                    content: '尊敬的VIP用户，是否订阅会员到期提醒？',
                    success: function (res) {
                        if (res.confirm) {
                            wx.requestSubscribeMessage({
                                tmplIds: item,
                                success(res){
                                    console.log(res)
                                    let time = new Date();
                                    let year = time.getFullYear();
                                    let month = time.getMonth()+1;
                                    let day = time.getDate();
                                    time = year+'-'+month+'-'+day;
                                    uni.setStorageSync('timeSubscribe',time)
                                }
                            })
                        }else {
                            _this.is_subscribe = false;
                        }
                    }
                })
            },
            //获取个人信息
            async getUserInfos(){
                let userToken=uni.getStorageSync('userToken');
                const res=await this.$api.getUserInfos({user_token:userToken});
                if(res.data.level=='success'){
                    this.userInfo=res.data.data
                    this.type = 1;
                    uni.setStorageSync('UserId',res.data.data.id);
                    uni.setStorageSync('is_Vip',res.data.data.user_info.is_member_unit);//判断是否单位入驻
                    uni.setStorageSync('is_unit_auth',res.data.data.user_info.is_unit_auth);
                    uni.setStorageSync('is_personal_auth',res.data.data.user_info.personal_auth_status);
                    let _this = this;
                    if (res.data.data.user_info.is_vip==1&&_this.is_subscribe){
                        _this.getTemplate();
                    }

                }
            },
            //获取个人信息
            async getUserInfoss(){
                let userToken=uni.getStorageSync('userToken');
                const res=await this.$api.getUserInfos({user_token:userToken});
                if(res.data.level=='success'){
                    this.userInfo=res.data.data
                    this.type = 1;
                    uni.setStorageSync('UserId',res.data.data.id);
                    uni.setStorageSync('is_Vip',res.data.data.user_info.is_member_unit);//判断是否单位入驻
                    uni.setStorageSync('is_unit_auth',res.data.data.user_info.is_unit_auth);
                    uni.setStorageSync('is_personal_auth',res.data.data.user_info.personal_auth_status);
                }
            },
            //获取订阅消息模板id
            async getTemplate(){
              const res = await this.$api.getTemplate({type: 'vip'});
              if (res.data.level == 'success'){
                  this.Subscribe(res.data.data);
              }
            },
            //授权登录
            async bindGetUserInfo(){
                var _this=this;
                uni.login({
                    success: res =>{
                        let code = res.code;
                        uni.getUserInfo({
                            success: async res => {
                                // console.log(res)
                                let params = {
                                    raw_data:res.rawData,
                                    signature:res.signature,
                                    code:code
                                }
                                const result = await _this.$api.wxAppLogin(params);
                                if (result.data.level == 'success'){
                                    uni.setStorageSync('userToken',result.data.data);
                                    const res2=await _this.$api.getUserInfos({user_token:result.data.data});
                                    if(res2.data.level=='success'){
                                        uni.setStorageSync('UserId',res2.data.data.id);
                                        uni.setStorageSync('is_Vip',res2.data.data.user_info.is_member_unit);//判断是否单位入驻
                                        uni.setStorageSync('is_unit_auth',res2.data.data.user_info.is_unit_auth);
                                        uni.setStorageSync('is_personal_auth',res2.data.data.user_info.personal_auth_status);
                                        this.userInfo=res2.data.data
                                        this.type = 1;
                                        if (res2.data.data.user_info.is_vip==1){
                                            _this.getTemplate();
                                        }
                                        // if (res2.data.data.phone==null){
                                        //     uni.showModal({
                                        //         title: '提示',
                                        //         content: '当前用户未绑定手机号，是否进入编辑页去绑定？',
                                        //         success: function (res3) {
                                        //             if (res3.confirm) {
                                        //                 uni.navigateTo({
                                        //                     url: '/pages/person/editInfo/editInfo'
                                        //                 })
                                        //             } else if (res3.cancel) {
                                        //                 uni.showToast({
                                        //                     icon:'none',
                                        //                     title: "未绑定手机号有部分功能无法使用！"
                                        //                 })
                                        //             }
                                        //         }
                                        //     });
                                        // }
                                    }

                                }
                            }
                        })
                    }
                })
            },
            //跳转我的关注页面
            skipMyFollow(id){
                if (uni.getStorageSync('userToken')){
                    switch (id) {
                        case 0:
                            uni.navigateTo({
                                url:'/pages/person/myFollow/myFollow?followId='+id
                            })
                            break;
                        case 1:
                            uni.navigateTo({
                                url:'/pages/person/myFollow/myFollow?followId='+id
                            })
                            break;
                        case 2:
                            uni.navigateTo({
                                url:'/pages/person/myFollow/myFollow?followId='+id
                            })
                            break;
                    }
                } else {
                    uni.showToast({
                        icon:'none',
                        title: "请先登录！",
                        duration: 3000
                    })
                }
            },
            //跳转绑定手机号页面
            skipLogin(){
                if (uni.getStorageSync('userToken')){
                    uni.navigateTo({
                        url: '/pages/login/login?phone='+this.userInfo.phone
                    })
                } else {
                    uni.showToast({
                        icon:'none',
                        title: "请先登录！",
                        duration: 3000
                    })
                }
            },
            //跳转到个人编辑页面
            skipEditPerson(){
                if (uni.getStorageSync('userToken')){
                    uni.navigateTo({
                        url: '/pages/person/editInfo/editInfo'
                    })
                } else {
                    uni.showToast({
                        icon:'none',
                        title: "请先登录！",
                        duration: 3000
                    })
                }
            },
            //跳转到发布的需求页面
            skipReleaseNeeds(){
                if (uni.getStorageSync('userToken')){
                    uni.navigateTo({
                        url: '/pages/person/releaseNeeds/releaseNeeds'
                    })
                } else {
                    uni.showToast({
                        icon:'none',
                        title: "请先登录！",
                        duration: 3000
                    })
                }
            },
            //跳转到对接的需求页面
            skipButtjointNeeds(){
                if (uni.getStorageSync('userToken')){
                    uni.navigateTo({
                        url: '/pages/person/buttjointNeeds/buttjointNeeds'
                    })
                } else {
                    uni.showToast({
                        icon:'none',
                        title: "请先登录！",
                        duration: 3000
                    })
                }
            },
            //跳转到我的活动页面
            skipMyActivity(){
                if (uni.getStorageSync('userToken')){
                    uni.navigateTo({
                        url: '/pages/person/myActivity/myActivity'
                    })
                } else {
                    uni.showToast({
                        icon:'none',
                        title: "请先登录！",
                        duration: 3000
                    })
                }
            },
            //跳转到认证页面
            skipAuthentication(){
                if (uni.getStorageSync('userToken')){
                    uni.navigateTo({
                        url: '/pages/Authentication/Authentication'
                    })
                } else {
                    uni.showToast({
                        icon:'none',
                        title: "请先登录！",
                        duration: 3000
                    })
                }
            },
            //跳转到关于我们页面
            skipAboutUs(){
                uni.navigateTo({
                    url: '/pages/person/aboutUs/aboutUs'
                })
            }
        }
    }
</script>

<style scoped lang="scss">
    @import '~@/static/scss/mixin.scss';
    .page{
        background: #F8F8F8;
        height: 100vh;
    }
    .page-header{
        padding: 0 30rpx;
        height: 408rpx;
        background: #3782FF;
        color: #ffffff;
        .page-header-top{
            @include flex(flex-start);
            flex-direction: row;
            padding: 60rpx 0;
            .header-top-left{
                width: 120rpx;
                height: 120rpx;
                margin: 0 30rpx 0 40rpx;
                position: relative;
                .img{
                    width: 100%;
                    height: 100%;
                    border-radius: 360rpx;
                }
                .icon{
                    position: absolute;
                    right: 0rpx;
                    width: 40rpx;
                    height: 40rpx;
                    bottom: 0;
                }
            }
            .header-top-center{
                @include flex(center,flex-start);
                flex-direction: column;
                height: 120rpx;
                max-width: calc(100% - 353rpx);
                .user-name{
                    height: 40rpx;
                    line-height: 40rpx;
                    font-size: 40rpx;
                    margin-bottom: 6rpx;
                    .icon{
                        width: 40rpx;
                        height: 40rpx;
                    }
                }
                .notloggedin{
                    height: 56rpx;
                    line-height: 56rpx;
                    font-size: 40rpx;
                    background: none;
                    color: #FFFFFF;
                    padding: 0;
                    border: none;
                }
                button::after{
                    border: none;
                }
                .user-phone{
                    height: 40rpx;
                    font-size: 28rpx;
                    line-height: 40rpx;
                }
                .user_unit_name{
                    height: 40rpx;
                    font-size: 28rpx;
                    line-height: 40rpx;
                    @include ellipsis(1)
                }
            }
            .header-top-right{
                margin-left: auto;
                @include flex();
                flex-direction: row;
                min-width: 143rpx;
                text{
                    height: 45rpx;
                    font-size: 28rpx;
                    line-height: 45rpx;
                }
                img{
                    width: 30rpx;
                    height: 30rpx;
                    margin-left: 17rpx;
                }
            }
        }
        .page-header-bottom{
            @include flex();
            flex-direction: row;
            padding: 8rpx 0 20rpx 0;
            .header-bottom-item{
                width: calc((100% - 4rpx)/3);
                height: 140rpx;
                @include flex();
                flex-direction: column;
                .item-number{
                    height: 45rpx;
                    line-height: 45rpx;
                    font-size: 32rpx;
                    margin-bottom: 10rpx;
                }
                .item-title{
                    height: 40rpx;
                    line-height: 40rpx;
                    font-size: 28rpx;
                }
            }
            .header-bottom-line{
                height: 60rpx;
                width: 2rpx;
                background: #6FA5FF;
            }
        }
    }
    .page-body-list{
        background: #ffffff;
        margin-bottom: 20rpx;
        button{
            background: #FFFFFF;
        }
        button::after{
            border: none;
        }
        .body-list-item{
            height: 120rpx;
            padding: 0 30rpx;
            @include flex(flex-start);
            flex-direction: row;
            border-bottom: 2rpx solid #E5E5E5;
            .list-item-img{
                width: 80rpx;
                height: 80rpx;
            }
            .list-item-title{
                margin-left: 16rpx;
                color: #333333;
                font-size: 32rpx;
            }
            .list-item-right{
                margin-left: auto;
                @include flex();
                flex-direction: row;
                text{
                    color: #999999;
                    font-size: 32rpx;
                    margin-right: 16rpx;
                }
                img{
                    width: 30rpx;
                    height: 30rpx;
                }
            }
        }
        .body-list-item:last-child{
            border-bottom: none;
        }
    }
    .page-body-list:last-child{
        margin-bottom: 0rpx;
    }
</style>
